package devbureau.fstore.util;

import java.util.*;
import java.io.*;

import org.apache.log4j.Logger;

public class PropertyHelper {

	private static final Logger logger = Logger.getLogger(PropertyHelper.class);

	private Properties properties = null;
	
   	private static String PROPERTIES_PATH = "c:/opt/store/conf/store.properties";
	private static PropertyHelper instance = null;
	
	
	private PropertyHelper() throws Exception {
		properties = new Properties();
		InputStream is = null;
		try {
			is = new FileInputStream(PROPERTIES_PATH);
			properties.load(is);
		} catch (Exception e) {
			logger.error(e, e);
			throw new Exception("Read configuration file error '"+PROPERTIES_PATH+"'");
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					// do nothing
					logger.error(e,e);
				}
			}
		}
	}

	private Properties getProperties() {
		return properties;
	}
	
	
	public static String getPropertyPath(){
		return PROPERTIES_PATH;
	}
	
	
	public static synchronized void setPropertyPath(String path) throws Exception {
		PROPERTIES_PATH = path;
		instance = new PropertyHelper();
	}

	public static String getProperty(String propertyName) throws Exception {
		if(instance == null) {
				instance = new PropertyHelper();
		}
		return instance.getProperties().getProperty(propertyName);
	}

	public static String getProperty(String propertyName, String defaultValue)  throws Exception {
		if(instance == null) {
			instance = new PropertyHelper();
		}
		return instance.getProperties().getProperty(propertyName, defaultValue);
	}
}
